package com.example.handlerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.squareup.leakcanary.RefWatcher;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private MyHandler myHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        test();
        findViewById(R.id.btn_second).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        myHandler = new MyHandler(new MyEventListener() {
            @Override
            public void handleMessage(Message msg) {
                Log.e(TAG, "handleMessage: " + MainActivity.this.hashCode());
            }
        });

        myHandler.sendEmptyMessage(100);

        findViewById(R.id.btn_refreshui).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RefreshUIActivity.class));
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = MyApplication.getRefWatcher(this);
        if (refWatcher != null) {
            refWatcher.watch(this);
        }
    }



    private ThreadLocal<Boolean> booleanThreadLocal = new ThreadLocal<>();

    /**
     * 测试ThreadLocal的使用
     */
    private void test() {
        booleanThreadLocal.set(true);
        Log.e(TAG, "booleanThreadLocal main :" + booleanThreadLocal.get());
        new Thread(new Runnable() {
            @Override
            public void run() {
                booleanThreadLocal.set(false);
                Log.e(TAG, "booleanThreadLocal 1:" + booleanThreadLocal.get());
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "booleanThreadLocal 2:" + booleanThreadLocal.get());
            }
        }).start();
    }

   private void useHanlderInSubThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                Handler h = new Handler(getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
                };
                Toast.makeText(MainActivity.this, "这是一个消息", Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Looper.loop();
            }
        }).start();


       new Thread(new Runnable() {
           @Override
           public void run() {
               Looper.prepare();
               Toast.makeText(MainActivity.this, "这是一个消息", Toast.LENGTH_SHORT).show();
               try {
                   Thread.sleep(5000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               Looper.loop();
           }
       }).start();

    }

}
